package com.icinema.main;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.icinema.client.CinemaService;
import com.icinema.client.CinemaServiceAsync;
import com.icinema.client.Message;
import com.icinema.vo.Cinema;

public class AddCinemaView extends Composite {
	TextBox txtCinemaName = new TextBox();
	TextBox txtCinemaManager = new TextBox();
	TextArea txtAddress = new TextArea();
	TextBox txtPhone = new TextBox();
	TextBox txtSeatCapicity = new TextBox();
	FlexTable cinemaTable = new FlexTable();
	private final CinemaServiceAsync cinemaService = GWT
	.create(CinemaService.class);
	
	public AddCinemaView(){
		VerticalPanel panel = new VerticalPanel();
		FlexTable table = new FlexTable();
		initWidget(panel);
		
		txtCinemaName.setStyleName("textBox");
		txtCinemaManager.setStyleName("textBox");
		txtAddress.setStyleName("textBox");
		txtPhone.setStyleName("textBox");
		txtSeatCapicity.setStyleName("textBox");
		
		
		table.setWidget(0,0, new Label("Cinema Name:"));
		table.setWidget(0,1,txtCinemaName);
		
		table.setWidget(1,0, new Label("Cinema Manager:"));
		table.setWidget(1,1,txtCinemaManager);		
		
		table.setWidget(2,0, new Label("Address:"));
		table.setWidget(2,1,txtAddress);
		
		table.setWidget(3,0, new Label("Cinema Phone:"));
		table.setWidget(3,1,txtPhone);		
		
		table.setWidget(4,0, new Label("Seat Capacity:"));
		table.setWidget(4,1,txtSeatCapicity);
		
		Button btnOK = new Button("OK");
		btnOK.addMouseDownHandler(new MouseDownHandler(){

			@Override
			public void onMouseDown(MouseDownEvent event) {
				
				Cinema cinema = new Cinema();
				cinema.setName(txtCinemaName.getText());
				cinema.setManager(txtCinemaManager.getText());
				cinema.setPhone(txtPhone.getText());
				cinema.setAddress(txtAddress.getText());
				cinema.setSeatCapacity(Long.parseLong(txtSeatCapicity.getText()));
				
				cinemaService.addCinema(cinema, new AsyncCallback<String>(){

					@Override
					public void onFailure(Throwable caught) {
					}

					@Override
					public void onSuccess(String result) {
						refresh();
					}
					
				});
			}
			
		});
		table.setWidget(5,1,btnOK);		
		panel.add(table);
		panel.add(cinemaTable);
	}
	
	public void refresh(){
		this.setVisible(false);
		this.setVisible(true);
	}
	
	@Override
	public void setVisible(boolean visible) {
		if(visible == true){
			cinemaTable.removeAllRows();
			
			cinemaTable.getRowFormatter().setStyleName(0,"tableViewBrowseHeaders");
			cinemaTable.setText(0,0,"Cinema Name");
			cinemaTable.setText(0,1,"Address");
			
			cinemaService.getCinemas(new AsyncCallback<List<Cinema>>(){

				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(List<Cinema> result) {
					for(int i=0;i<result.size();i++){
						cinemaTable.setText(i+1, 0,result.get(i).getName());
						cinemaTable.setText(i+1, 1,result.get(i).getAddress());
						Image imgDelete = new Image("/images/delete.png");
						imgDelete.setStyleName("imgButton");
						String showId = result.get(i).getId();
						imgDelete.addMouseDownHandler(new ImageDeleteMouseDownHandler(showId));
						cinemaTable.setWidget(i+1,2, imgDelete);
						cinemaTable.getCellFormatter().setStyleName(i+1, 0, "tableCell");
						cinemaTable.getCellFormatter().setStyleName(i+1, 1, "tableCell");
					}					
				}
				
			});
			
		}
		super.setVisible(visible);
	}
	class ImageDeleteMouseDownHandler implements  MouseDownHandler{
		private String movieShowingId; 
		public ImageDeleteMouseDownHandler(String movieShowingId){
			this.movieShowingId = movieShowingId;
		}
		@Override
		public void onMouseDown(MouseDownEvent event) {
			if(!Window.confirm("This will Delete the Cinema")){
				return;
			}
			cinemaService.deleteCinema(movieShowingId, new AsyncCallback<Message>(){

				@Override
				public void onFailure(Throwable caught) {
				}

				@Override
				public void onSuccess(Message result) {
					if(!result.getErr_msg().equals("")){
						Window.alert(result.getErr_msg());
					}
				}
			});
			refresh();
		}
	}	
	
	
}
